import { defineStore } from "pinia";
import { ref } from "vue";
import { getCurrentUser } from "@/api/CsrAuthApi";

export const useLoginUserStore = defineStore("counter", () => {
  const loginUser = ref<any>({
    token: "",
    name: "",
    avatar: "",
    roles: [],
    permissions: [],
  });

  // 获取当前登录用户信息
  async function fetchLoginUser(token: any) {
    if (token) {
      const res = await getCurrentUser(token);
      loginUser.value.token = token;
      loginUser.value.name = res.data.data.nickName;
      loginUser.value.avatar = res.data.data.avatar;
      loginUser.value.roles = res.data.data.roles;
      loginUser.value.permissions = res.data.data.permissions;
    }
  }

  return { loginUser, fetchLoginUser };
});
